package ru.megazlo.basemvc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.megazlo.basemvc.dao.repository.NewsRepository;
import ru.megazlo.basemvc.dto.view.NewsEdit;
import ru.megazlo.basemvc.dto.view.NewsListView;

@Service
@Transactional
public class NewsServiceImpl extends BaseServiceImpl implements NewsService {

	@Autowired
	private NewsRepository newsRepository;

	@Override
	public NewsEdit saveNews(NewsEdit newsEdit) {
		return newsEdit;
	}

	@Transactional(readOnly = true)
	public Page<NewsListView> getNewsByPage(Integer page) {
		Pageable pg = new PageRequest(page, pageSize);
		return newsRepository.findShortNews(pg);
	}

	public String getFirstParagraph(String text) {
		if (text == null || text.length() == 0) {
			return "";
		}
		int endPar = text.indexOf("</p>");
		return text.substring(0, endPar);
	}
}
